
package com.akupilih.app;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Images;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.akupilih.app.model.DBClass;
import com.akupilih.app.model.Utils;
import com.facebook.HttpMethod;
import com.facebook.Session;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;

public class StempFragment extends Fragment implements OnClickListener {
    Bitmap b1, b2, result;
    private FragmentActivity myContext;
    String url_foto;
    ImageView img;
    private ProgressBar pBar;
    private Button share;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View rootView = inflater.inflate(R.layout.fragment_stamp, container,
                false);

        DBClass db = new DBClass(myContext);
        String vote = db.getVote();
        Log.d("vote", vote);
        if (vote.equals("1")) {
            b2 = BitmapFactory.decodeResource(this.getResources(),
                    R.drawable.stamp_1);
        } else {
            b2 = BitmapFactory.decodeResource(this.getResources(),
                    R.drawable.stamp_2);
        }

        Session session = Session.getActiveSession();

        share = (Button) rootView.findViewById(R.id.btn_share_stamp);
        img = (ImageView) rootView.findViewById(R.id.stamp);
        // Button btnHasil = (Button) rootView.findViewById(R.id.share);
        pBar = (ProgressBar) rootView.findViewById(R.id.pbar);

        share.setOnClickListener(this);

        getUserPicture(session);

        return rootView;
    }

    private void combineImage(String URL_FOTO_FB) {
        ImageLoader.getInstance().loadImage(URL_FOTO_FB,
                new SimpleImageLoadingListener() {
                    @Override
                    public void onLoadingComplete(String imageUri, View view,
                            Bitmap loadedImage) {
                        // TODO Auto-generated method stub
                        img.setVisibility(View.VISIBLE);
                        share.setVisibility(View.VISIBLE);
                        super.onLoadingComplete(imageUri, view, loadedImage);
                        b1 = loadedImage;
                        Bitmap scale = Bitmap.createScaledBitmap(b1,
                                b2.getWidth(), b2.getHeight(), false);
                        result = overlay(b2, scale);
                        img.setImageBitmap(result);
                        pBar.setVisibility(View.GONE);
                    }
                });
        pBar.setVisibility(View.VISIBLE);
        img.setVisibility(View.GONE);
        share.setVisibility(View.GONE);
    }

    @Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
        myContext = (FragmentActivity) activity;

    }

    public static Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {

        Bitmap bmOverlay = Bitmap.createBitmap(bmp2.getWidth(),
                bmp2.getHeight(), bmp2.getConfig());
        Canvas canvas = new Canvas(bmOverlay);

        Matrix m = new Matrix();
        canvas.drawBitmap(bmp2, m, null);
        canvas.drawBitmap(bmp1, m, null);

        return bmOverlay;
    }

    private void getUserPicture(Session session) {
        Bundle params = new Bundle();
        params.putBoolean("redirect", false);
        params.putString("type", "large");

        new com.facebook.Request(session, "/me/picture", params,
                HttpMethod.GET, new com.facebook.Request.Callback() {

                    @Override
                    public void onCompleted(com.facebook.Response response) {
                        JSONObject jsonObject = null;
                        try {

                            jsonObject = new JSONObject(response
                                    .getGraphObject().getInnerJSONObject()
                                    .toString());
                            JSONObject obj = jsonObject.getJSONObject("data");
                            url_foto = obj.getString("url");
                            combineImage(url_foto);

                        } catch (JSONException e) {
                            Utils.TRACE(
                                    "MenuUtamaActivity.getUserPicture(...).new Callback() {...}",
                                    "onCompleted : " + e.getMessage());
                        }
                    }

                }).executeAsync();
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        if (arg0 == share) {
            Toast.makeText(getActivity(), "asfsafas", Toast.LENGTH_SHORT).show();

            String pathofBmp = Images.Media.insertImage(
                    getActivity().getContentResolver(), result, "title", null);
            Uri bmpUri = Uri.parse(pathofBmp);

            final Intent emailIntent1 = new Intent(
                    android.content.Intent.ACTION_SEND);
            emailIntent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            emailIntent1.putExtra(Intent.EXTRA_STREAM, bmpUri);
            emailIntent1.setType("image/jpeg");
            startActivity(emailIntent1);
        }

    }
}
